import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import List from '../views/List.vue'
import Sco from '../views/Sco.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path:'/login',
    name:'Login',
    component:Login
  },
  {
    path:'/list',
    name:'List',
    component:List
  },
  {
    path:'/sco',
    name:'Sco',
    component:Sco
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.name != 'Login'){
    if(!localStorage.getItem('usr')){
      next({name:'Login'})
    }else{
      next()
    }
  }else{
    if(localStorage.getItem('usr')){
      next({name:'Home'})
    }else{
      next()
    }
  }
  // if(to.path !== "/login"){
  //   if(localStorage.getItem('usr')){
  //     next();
  //   }else{
  //     next({
  //       path:'/login',
  //       query: {redirect:to.fullPath}
  //     })
  //   }
  // }else{
  //   if(localStorage.getItem('usr')){
  //     next('/')
  //   }else{
  //     next();
  //   }
  // }
})

export default router
